<%--
  Created by IntelliJ IDEA.
  User: SamZhao
  Date: 2016/12/6
  Time: 19:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@ include file="/WEB-INF/jsp/include.inc.jsp"%>
<script src="/js/jquery-1.8.3.min.js" ></script>

<!-- BEGIN MAIN CONTENT -->
<section id="main-content">
    <!-- BEGIN WRAPPER  -->
    <section class="wrapper">
        <!-- BEGIN ROW  -->
        <div class="row">
            <div class="col-lg-12" style="width: 800px;">
                <section class="panel">
                    <header class="panel-heading">
                        <span class="label label-primary">书籍变更</span>
                        <span class="tools pull-right">
                           <a href="javascript:;" class="fa fa-chevron-down"></a>
                           <a href="javascript:;" class="fa fa-times"></a>
                           </span>
                    </header>
                    <div class="panel-body">
                        <div class="modal fade" id="myModal3" >
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                        <h4 class="modal-title">错误!</h4>
                                    </div>
                                    <div class="modal-body" id="erroText">
                                    </div>
                                    <div class="modal-footer">
                                        <button class="btn btn-danger" type="button" data-dismiss="modal"> Ok</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="modal fade" id="myModal"  >
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                        <h4 class="modal-title">Modal Tittle</h4>
                                    </div>
                                    <div class="modal-body">
                                       提交成功!
                                    </div>
                                    <div class="modal-footer">
                                        <button class="btn btn-success" type="button" data-dismiss="modal">确定</button>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class=" form">
                            <form class="cmxform form-horizontal tasi-form" id="commentForm" method="get" action="/book/saveBook.html">
                                <input id="id" name="id"  type="hidden" value="${book.id }"/>
                                <div class="form-group ">
                                    <label for="name" class="control-label col-lg-2">书籍名称</label>
                                    <div class="col-lg-10">
                                        <input class=" form-control" id="name" name="name"  type="text" required value="${book.name }"/>
                                    </div>
                                </div>
                                <div class="form-group ">
                                    <label for="price" class="control-label col-lg-2">价格</label>
                                    <div class="col-lg-10">
                                        <input class="form-control " id="price" type="text" name="price" required value="${book.price }"/>
                                    </div>
                                </div>
                                <div class="form-group ">
                                    <label for="author" class="control-label col-lg-2">作者</label>
                                    <div class="col-lg-10">
                                        <input class="form-control " id="author" type="text" name="author" value="${book.author }"/>
                                    </div>
                                </div>
                                <div class="form-group ">
                                    <label for="press" class="control-label col-lg-2">出版社</label>
                                    <div class="col-lg-10">
                                        <input class="form-control " id="press" type="text" name="press" value="${book.press }" />
                                    </div>
                                </div>
                                <div class="form-group ">
                                    <label for="totalNum" class="control-label col-lg-2">入库数量</label>
                                    <div class="col-lg-10">
                                        <input class="form-control " id="totalNum" type="text" name="totalNum" value="${book.totalNum }"/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-lg-offset-2 col-lg-10">
                                        <button class="btn btn-primary" type="button" id="saveBtn">保存</button>
                                        <button class="btn btn-default" type="button" onclick="cancelBtn();">取消</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </section>
            </div>
        </div>
        <!-- END ROW  -->
    </section>
</section>
<script type="text/javascript">
    function cancelBtn(){
        window.location = "/book/list.html";
    }
    $(function(){
        $("#myModal3").modal("hide");
        $("#myModal").modal("hide");
        // save
        $("#saveBtn").on('click', function() {
            if($("#name").val()==""){
                $("#erroText").html("书籍名称不能为空!");
                $("#myModal3").modal("show");
            }
            else if($("#price").val()==""){
                $("#erroText").html("价格不能为空!");
                $("#myModal3").modal("show");
            }
            else if($("#author").val()==""){
                $("#erroText").html("作者不能为空!");
                $("#myModal3").modal("show");
            }
            else if($("#press").val()==""){
                $("#erroText").html("出版社不能为空!");
                $("#myModal3").modal("show");
            }
            else if($("#totalNum").val()==""){
                $("#erroText").html("入库数量不能为空!");
                $("#myModal3").modal("show");
            }
            else{
                var flag = checkUser();
                if(flag == false ){
                    $.ajax({
                        type: "POST",
                        url: "/book/saveBook.html",
                        data:$('#commentForm').serialize(),// 你的formid
                        async: false,
                        error: function() {
                            $("#erroText").html("网络异常!");
                            $("#myModal3").modal("show");
                        },
                        success: function(data) {

                            if(data == 'success'){
                                $("#myModal").modal("show");

                                window.setInterval(function()//延迟进行跳转
                                {
                                    intervalRun();
                                }, 1000);
                            }else{
                                $("#erroText").html("新增出错!");
                                $("#myModal3").modal("show");
                            }

                        }
                    });
                }else{
                    $("#erroText").html("该书籍已存在!");
                    $("#myModal3").modal("show");
                }

            }
        });

        function intervalRun(){
            $("#myModal").modal("hide");
            window.location = "/book/list.html";
        }

        function checkUser(){
            var flag = false;
            $.ajax({
                type: "POST",
                url: "/book/checkBook.html",
                data:$('#commentForm').serialize(),// 你的formid
                async: false,
                error: function() {
                    $("#erroText").html("网络异常!");
                    $("#myModal3").modal("show");
                },
                success: function(data) {
                    if(data == 'success')
                        flag = true;
                }
            });
            return flag;
        }

    });

</script>
